package com.dwxt.community.service;

import com.dwxt.common.base.*;
import com.dwxt.common.util.*;
import com.dwxt.community.dao.*;
import com.dwxt.community.entity.*;
import com.github.pagehelper.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.util.*;

@Service
public class CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserService userService;

    /**
     * 评论列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public BaseResult get(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return BaseResult.ok(new PageInfo<Comment>(commentMapper.sellectAll()));
    }

    /**
     * 添加评论
     * @param comment
     * @return
     */
    public BaseResult insert(Comment comment) {
        //获取当前用户
        User user = userService.selectCurrUserByPrimaryKey();
        comment.setUserId(user.getId());
        comment.setUserName(user.getUsername());
        comment.setUserPhone(user.getPhone());
        comment.setId(IDUtils.genItemId());
        //插入数据
        int i = commentMapper.insertSelective(comment);
        return BaseResult.build(i>0?StatusCode.SUCCESS:StatusCode.FAIL);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public BaseResult del(String id) {
        return BaseResult.ok(commentMapper.deleteByPrimaryKey(id));
    }

    /**
     * 更新
     * @param comment
     * @return
     */
    public BaseResult update(Comment comment) {
        //获取当前用户
        User user = userService.selectCurrUserByPrimaryKey();
        comment.setUserId(user.getId());
        comment.setUserName(user.getUsername());
        comment.setUserPhone(user.getPhone());
        int i = commentMapper.updateByPrimaryKey(comment);
        return BaseResult.build(i>0?StatusCode.SUCCESS:StatusCode.FAIL);
    }

    /**
     * 根据用户ID获取评论
     * @param userId
     * @return
     */
    public BaseResult getByUserId(Integer pageNum, Integer pageSize,String userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> commentList = commentMapper.selectByUserId(userId);
        return BaseResult.ok(new PageInfo<>(commentList));
    }
    /**
     * 根据资讯ID获取评论
     * @param informationId
     * @return
     */
    public BaseResult getByInformationId(Integer pageNum, Integer pageSize,String informationId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> commentList = commentMapper.selectByInformationId(informationId);
        return BaseResult.ok(new PageInfo<>(commentList));
    }

    /**
     * 搜索
     * @param content
     * @param username
     * @return
     */
    public BaseResult search(Integer pageNum, Integer pageSize,String content, String username) {
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> commentList = commentMapper.search(content,username);
        return BaseResult.ok(new PageInfo<>(commentList));
    }

    /**
     * 根据资讯ID删除评论
     * @param id
     * @return
     */
    public int delByInformationId(String id) {
        return commentMapper.delByInformationId(id);
    }
}
